package linter.linterjava.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import linter.linterjava.POJO.DO.ArticleReview;
import linter.linterjava.POJO.DO.ReviewDislikes;
import linter.linterjava.POJO.DO.ReviewLikes;
import linter.linterjava.POJO.DTO.ReviewDislikesDTO;
import linter.linterjava.mapper.ArticleReviewMapper;
import linter.linterjava.mapper.ReviewLikesMapper;
import linter.linterjava.mapper.ReviewReportMapper;
import linter.linterjava.service.ReviewDislikesService;
import linter.linterjava.mapper.ReviewDislikesMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

import static linter.linterjava.custom.CustomResultType.*;

/**
* @author 北煜
* @description 针对表【review_dislikes(评论不喜欢表)】的数据库操作Service实现
* @createDate 2024-07-07 12:16:01
*/
@Service
public class ReviewDislikesServiceImpl extends ServiceImpl<ReviewDislikesMapper, ReviewDislikes>
    implements ReviewDislikesService{

    @Resource
    ArticleReviewMapper articleReviewMapper;
    @Resource
    ReviewDislikesMapper reviewDislikesMapper;
    @Resource
    ReviewReportMapper reviewReportMapper;
    @Resource
    ReviewLikesMapper reviewLikesMapper;

    @Override
    public String dislikeReview(Long uuid, ReviewDislikesDTO reviewDislikesDTO) {
        Date date = new Date();
        ReviewDislikes reviewDislikes = new ReviewDislikes();
        ArticleReview isExistList = articleReviewMapper.selectById(reviewDislikesDTO.getReviewId());
        if (isExistList == null){ return COMMENT_NOT_EXIST; }
        if (reviewReportMapper.selectById(reviewDislikesDTO.getReviewId())!= null){
            Integer isReport = reviewReportMapper.selectById(reviewDislikesDTO.getReviewId()).getResultOfHanding();
            if ( isReport == null){ isReport = 0; }
            if (isReport == 1 && isReport == 2){ return COMMENT_REPORTED; }
        }
        reviewDislikes.setReviewId(reviewDislikesDTO.getReviewId());
        reviewDislikes.setUserId(uuid);
        reviewDislikes.setCreateTime(date);
        reviewDislikes.setUpdataTime(date);
        int flag = reviewDislikesMapper.insert(reviewDislikes);
        ArticleReview articleReview = new ArticleReview();
        Integer reviewDislikeNub = articleReviewMapper.selectById(reviewDislikesDTO.getReviewId()).getReviewDislikeNub();
        articleReview.setReviewLikeNub(reviewDislikeNub + 1);
        articleReview.setUpdataTime(date);
        flag += articleReviewMapper.update(articleReview, new QueryWrapper<ArticleReview>()
                .eq("id", reviewDislikesDTO.getReviewId()).eq("is_delete", 0));
        return flag > 0 ? REPORT_SUCCESS : REPORT_ERROR;

    }
}




